from rest_framework import serializers

from course.models import CategoryModel, CourseModel


class Category(serializers.ModelSerializer):
    class Meta:
        model = CategoryModel
        fields = ["cname","id"]


class CategorySerializer(serializers.ModelSerializer):
    children = Category(many=True)
    class Meta:
        model = CategoryModel
        fields = '__all__'

class CourseSerializer(serializers.ModelSerializer):
    tea = serializers.SerializerMethodField(read_only=True)
    def get_tea(self,obj):
        return {"tname":obj.teacher.tname,"job":obj.teacher.job,"headimg":"http://127.0.0.1:8000"+str(obj.teacher.headimg)}
    class Meta:
        model = CourseModel
        fields = '__all__'

# class BrandImage(serializers.ModelSerializer):
#     img = serializers.ImageField()
#
# class BannerImage(serializers.Serializer):
#     img = serializers.ImageField()
#     def valdate(self,attrs):
#         print(attrs)
#         return attrs

from drf_haystack.serializers import HaystackSerializer
from .search_indexes import CourseIndex

class CourseIndexSerializer(HaystackSerializer):
    class Meta:
        index_classes = [CourseIndex]
        fields = ["id","cname","text","content",'cover']